<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form name="loginForm" method="">
        <div>
            <label for="" id="honeInput">手机</label>
            <input type="text" name="phone" id="phoneInput" placeholder="请输入手机号码" 
            required pattern="/^[0-9]{11}$/" >
            
        </div>
        <div>
            <button type="submit">提交</button>
        </div>
    </form>
    <script>
        const oForm=document.forms["loginForm"];
        oForm.addEventListener('submit',function(event){
            event.preventDefault();
            // console.log(oForm["phone"]);
            const phone=oForm["phone"].value.trim();
            console.log(phone);
            // 为空的话
            // required html5 input才有的属性
            if(!phone.length){
                console.log('手机号不能为空');
                return;
            }
            if (!/^[0-9]{11}$/.test(phone)){
                alert('手机号格式有误!');
                return;
            }
            // this 在事件中指向事件发生的对象
            this.submit();
            // console.log(phone);

        })
    </script>
</body>
</html>